using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;
using System.Runtime.InteropServices;

namespace FakeGPS_Helper
{
    public partial class frmFakeGPSHelper : Form
    {
        string FakeGPSFilesDir;
        RegistryKey rGPS;

        public frmFakeGPSHelper()
        {
            InitializeComponent();
            RegistryKey rFake;
            rFake = Registry.LocalMachine.OpenSubKey(@"\Security\AppInstall\Microsoft Corporation Fake GPS\ExecutableFiles\", true);
            
            if (rFake==null)
            {
                MessageBox.Show("This application requires Fake GPS to be installed.");
                this.Close();
            }

            FakeGPSFilesDir = (string)System.IO.Path.GetDirectoryName(rFake.GetValueNames()[0]) + @"\GPSFiles\";

            rGPS=Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\GPS Intermediate Driver\", true);
            if (rGPS.GetValue("CurrentLogFile") == null)
                cboLoggingEnabled.SelectedItem = "Disabled";
            else
                cboLoggingEnabled.SelectedItem = "Enabled";

            txtCurrentLogFile.Text = (string)rGPS.GetValue("FGHCurrentLogFile", rGPS.GetValue("CurrentLogFile","").ToString().Replace(FakeGPSFilesDir, "")).ToString().Replace(FakeGPSFilesDir, "");
            txtOldLogFile.Text = (string)rGPS.GetValue("FGHOldLogFile", rGPS.GetValue("OldLogFile","").ToString().Replace(FakeGPSFilesDir, "")).ToString().Replace(FakeGPSFilesDir, "");
            txtMaxLogFileSize.Text = (string)rGPS.GetValue("FGHMaxLogFileSize", rGPS.GetValue("MaxLogFileSize","").ToString()).ToString();

            cboLoggingEnabled.Focus();
        }

        private void cboLoggingEnabled_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoggingEnabled.SelectedItem != null)
                if ((string)cboLoggingEnabled.SelectedItem == "Enabled")
                    pnlLoggingDetails.Enabled = true;
                else
                    pnlLoggingDetails.Enabled = false;
        }

        private void mnuCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuDone_Click(object sender, EventArgs e)
        {
            if (txtCurrentLogFile.Text.IndexOf(@"\") > 0 || txtOldLogFile.Text.IndexOf(@"\") > 0)
            {
                MessageBox.Show("File names must not include path information.");
                return;
            }

            if (!txtCurrentLogFile.Text.EndsWith(".txt"))
                txtCurrentLogFile.Text = txtCurrentLogFile.Text + ".txt";

            if (!txtOldLogFile.Text.EndsWith(".txt"))
                txtOldLogFile.Text = txtOldLogFile.Text + ".txt";
            try
            {
                if (long.Parse(txtMaxLogFileSize.Text)==0)
                    MessageBox.Show("MaxLogFileSize must be numeric.");
            }
            catch
            {
                MessageBox.Show("MaxLogFileSize must be numeric.");
                return;
            }

            if ((string) cboLoggingEnabled.SelectedItem == "Disabled")
            {
                rGPS.DeleteValue("CurrentLogFile");
                rGPS.DeleteValue("OldLogFile");
                rGPS.DeleteValue("MaxLogFileSize");
            }
            else
            {
                rGPS.SetValue("CurrentLogFile", FakeGPSFilesDir + txtCurrentLogFile.Text,RegistryValueKind.String);
                rGPS.SetValue("OldLogFile", FakeGPSFilesDir + txtOldLogFile.Text, RegistryValueKind.String);
                rGPS.SetValue("MaxLogFileSize", txtMaxLogFileSize.Text, RegistryValueKind.DWord);
            }

            rGPS.SetValue("FGHCurrentLogFile", FakeGPSFilesDir + txtCurrentLogFile.Text);
            rGPS.SetValue("FGHOldLogFile", FakeGPSFilesDir + txtOldLogFile.Text);
            rGPS.SetValue("FGHMaxLogFileSize", txtMaxLogFileSize.Text);

            rGPS.Flush();
            rGPS.Close();

            if (MessageBox.Show("To complete this change, your device requires a soft reset (reboot), are you sure?", "Fake GPS Helper", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                SoftReset.Reset.ResetUnit();
            else
                rGPS = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\GPS Intermediate Driver\", true);
        }

    }
}
namespace SoftReset
{
    static class Reset
    {
        public static void ResetUnit()
        {
            int bytesReturned = 0;

            KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, null, 0, ref bytesReturned);
        }

        [DllImport("coredll.dll", SetLastError = true)]
        internal static extern bool KernelIoControl(UInt32 dwIoControlCode,
        IntPtr lpInBuf,
        Int32 nInBufSize,
        byte[] lpOutBuf,
        Int32 nOutBufSize,
        ref Int32 lpBytesReturned);

        internal const UInt32 IOCTL_HAL_REBOOT = 0x0101003C;
    }
}
